package com.itheima;

import com.itheima.dao.GoodsDao;
import com.itheima.pojo.Goods;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo02DocCRUD {

    @Autowired
    private GoodsDao goodsDao;

    //1.新增
    @Test
    public void save() {
        Goods goods = new Goods(1L, "炒米手机", "手机", "小米", 1222.0, "图片地址");
        goodsDao.save(goods);
    }

    //2.修改 save方法不变 id不变 内容改变
    @Test
    public void update() {
        Goods goods = new Goods(1L, "炒米手机", "手机", "小米", 19990.0, "图片地址");
        goodsDao.save(goods);
    }

    //3.删除
    @Test
    public void delete() {
        Goods good = new Goods();
        good.setId(1L);
        goodsDao.delete(good);
    }

    //4.根据id查询
    @Test
    public void findById() {
        Goods good = goodsDao.findById(2l).get();
        System.out.println(good);
    }

    //5.查询所有
    @Test
    public void findAll() {
        Iterable<Goods> goods = goodsDao.findAll();
        for (Goods good : goods) {
            System.out.println(good);
        }
    }

    //6.批量新增
    @Test
    public void saveAll() {
        List<Goods> goodList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Goods good = new Goods();
            good.setId((long) i);
            good.setTitle("[" + i + "]小米手机");
            good.setCategory("手机");
            good.setBrand("小米");
            good.setPrice(19999.0 + i);
            good.setImages("http://image.leyou.com/12479122.jpg");
            goodList.add(good);
        }
        goodsDao.saveAll(goodList);
    }

    //7.分页查询
    //8.查询排序
    @Test
    public void findByPageable() {
        //设置排序(排序方式，正序还是倒序，排序的id)
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        int currentPage = 2;//当前页
        int pageSize = 10;//每页显示多少条
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);
        //分页查询
        Page<Goods> goodPage = goodsDao.findAll(pageRequest);
        for (Goods good : goodPage.getContent()) {
            System.out.println(good);
        }
    }

    //9.term查询、match查询，match_all查询...
    @Test
    public void termQuery() {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");
        Iterable<Goods> goods = goodsDao.search(termQueryBuilder);
        for (Goods g : goods) {
            System.out.println(g);
        }
    }

    /**
     * term查询加分页
     */
    @Test
    public void termQueryByPage() {
        int currentPage = 0;
        int pageSize = 5;
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");
        Iterable<Goods> goods = goodsDao.search(termQueryBuilder, pageRequest);
        for (Goods g : goods) {
            System.out.println(g);
        }
    }
}






















